<?php
/**
 * Created by PhpStorm.
 * User: h
 * Date: 2020/12/23
 * Time: 20:07
 */

namespace app\api\controller;

use think\Db;

use app\admin\model\ProCate;
use app\admin\model\Pro as Promodel;
class Pro
{
    /**
     * @api {post/get} api/pro/pro 获取产品信息
     * @apiGroup Group
     * @apiDescription 获取产品信息
     * @apiParam {int} cate_id 产品分类id
     * @apiParamExample {json} request-example
     * {
     *  "cate_id": 1
     * }
     * @apiError {String} message 错误信息
     * @apiErrorExample  {json} error-example
     * {
     *   "message": "id不存在"
     * }
     * @apiSuccess {id} id 编号
     * @apiSuccess {String} name 产品名称
     * @apiSuccess {String} title 产品标题
     * @apiSuccess {int} cate_id 产品分类id
     * @apiSuccess {String} thumb 产品图片
     * @apiSuccess {String} create_time 创建时间
     * @apiSuccess {String} update_time 更新时间
     * @apiSuccess {String} content 产品简介
     * @apiSuccess {int} status 状态
     * @apiSuccessExample  {json} success-example
    {
    "code": 200,
    "meg": "请求成功",
    "data": [
    {
    "id": 1,
    "name": "DIN标准滚珠丝杠",
    "title": "螺母的法兰形状、安装孔以及额定负荷遵照DIN69051。",
    "cate_id": 1,
    "thumb": 30,
    "create_time": 0,
    "update_time": 1608539588,
    "content": "<p>的故事感动还是决定回家看书的空间</p>",
    "status": 0
    }
    ]
    }
     */
    public function pro()
    {
        $cate_id = input('cate_id');
        if (empty($cate_id)) {
            return json(['code' => 0, 'msg' => '请传入id']);
        }
        $catemodel = new ProCate();
        $cate_name = $catemodel->where('id', $cate_id)->find();
        if (!isset($cate_name) || empty($cate_id)) {
            return json(['code' => 0, 'msg' => 'id不存在']);
        }
        $promodel = new Promodel();
        $pro = $promodel->where('cate_id', $cate_id)->select();
        foreach ($pro as $v){
            $v['content']=strip_tags(str_replace('&nbsp;','',$v['content']));
            $v['create_time']=date('Y-m-d',$v['create_time']);
            $v['update_time']=date('Y-m-d',$v['update_time']);
        }
        if ($pro) {
            return json(['code' => 200, 'meg' => '请求成功', 'data' => $pro]);
        } else {
            return json(['code' => 0, 'msg' => '请求失败']);
        }

    }
}
